//
//  QYGMineTableViewCell.m
//  wawaji_ios
//
//  Created by yiguo qi on 2017/11/30.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGMineTableViewCell.h"

@interface QYGMineTableViewCell()

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UISwitch *sw;
@property (nonatomic, strong) UIImageView *nextView;
@end

@implementation QYGMineTableViewCell

- (void)setIsOn:(BOOL)isOn{
    
    _isOn = isOn;
    [self.sw setOn:isOn];
}
- (void)setIsHiddenLine:(BOOL)isHiddenLine{
    _isHiddenLine = isHiddenLine;
    self.lineView.hidden = self.isHiddenLine;
}
- (void)setMineAccessoryType:(QYGMineTableViewCellAccessoryType)mineAccessoryType{
    
    _mineAccessoryType = mineAccessoryType;
    switch (mineAccessoryType) {
        case QYGMineTableViewCellAccessoryNone:
        {
            self.sw.hidden = YES;
            self.nextView.hidden = YES;
        }
            break;
        case QYGMineTableViewCellAccessoryDisclosureIndicator:
        {
            self.sw.hidden = YES;
            self.nextView.hidden = NO;
        }
            break;
        case QYGMineTableViewCellAccessoryDetailSwitch:
        {
            self.sw.hidden = NO;
            self.nextView.hidden = YES;
        }
            break;
        default:
            break;
    }
}
- (void)setDetailStr:(NSString *)detailStr{
    _detailStr = detailStr;
    self.detailLabel.hidden = detailStr.length ? NO : YES;
    self.detailLabel.text = detailStr;
    [self.detailLabel sizeToFit];
}
- (void)setNameStr:(NSString *)nameStr{
    
    _nameStr = nameStr;
    self.nameLabel.text = nameStr;
    [self.nameLabel sizeToFit];
}
- (void)setIconStr:(NSString *)iconStr{
    
    _iconStr = iconStr;
    self.iconView.image = [UIImage imageNamed:iconStr];
    
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self initSubView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
    
}


#pragma mark target
- (void)swChangeValue:(UISwitch *)sw{
    
    QYGMineTableViewCell *mineTableViewCell = (QYGMineTableViewCell *)[sw superview];
    if(self.swChangeBlock)
    {
        self.swChangeBlock(mineTableViewCell, sw.isOn);
    }
}
#pragma mark setUI

- (void)initSubView{
    [self addSubview:self.iconView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.sw];
    [self addSubview:self.lineView];
    [self addSubview:self.nextView];
    [self addSubview:self.detailLabel];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.iconView.centerY = self.height * 0.5;
    self.iconView.x = ScaleW(20);
    
    self.nameLabel.x = CGRectGetMaxX(self.iconView.frame) + ScaleW(10);
    self.nameLabel.centerY = self.iconView.centerY;
    
    self.lineView.x = ScaleW(15);
    self.lineView.height = 1;
    self.lineView.y = self.height - self.lineView.height;
    self.lineView.width = self.width - 2 * ScaleW(15);
    
    self.sw.x = self.width - ScaleW(10) - self.sw.width;
    self.sw.centerY = self.iconView.centerY;
    
    self.nextView.x = self.width - ScaleW(15) - self.nextView.width;
    self.nextView.centerY = self.iconView.centerY;
    
    self.detailLabel.x = self.nextView.x - ScaleW(5) - self.detailLabel.width;
    self.detailLabel.centerY = self.iconView.centerY;
}
#pragma mark lazy
- (UISwitch *)sw{
    if(!_sw){
        _sw = [[UISwitch alloc] init];
        [_sw addTarget:self action:@selector(swChangeValue:) forControlEvents:UIControlEventTouchUpInside];
        _sw.onTintColor = [UIColor cm_mainColor];
        _sw.size = CGSizeMake(ScaleW(51), ScaleW(31));
    }
    return _sw;
}
- (UILabel *)nameLabel{
    
    if(!_nameLabel){
        
        _nameLabel = [UILabel labelWithText:@"" setFont:ScaleFont(16) setTextColor:[UIColor colorWithHex:@"333333"]];
    }
    return _nameLabel;
}
- (UIImageView *)iconView{
    
    if(!_iconView){
        _iconView = [[UIImageView alloc] init];
        _iconView.size = CGSizeMake(ScaleW(22), ScaleW(22));
    }
    return _iconView;
}
- (UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithHex:@"e1e4eb"];
    }
    return _lineView;
}
- (UIImageView *)nextView{
    
    if(!_nextView){
        _nextView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_go"]];
        _nextView.size = CGSizeMake(ScaleW(22), ScaleW(22));
    }
    return _nextView;
}

- (UILabel *)detailLabel{
    
    if(!_detailLabel){
        
        _detailLabel = [UILabel labelWithText:@"" setFont:ScaleFont(14) setTextColor:[UIColor colorWithHex:@"ffb52b"] setTextAlignment:NSTextAlignmentRight];
        _detailLabel.numberOfLines = 1;
        _detailLabel.width = ScaleW(200);
    }
    return _detailLabel;
}
@end
